package com.opc.work;

import cn.hutool.core.date.DateUtil;
import com.opc.dto.DeviceRecordTimesDto;
import com.opc.entity.ConsumeStatistic;
import com.opc.service.ConsumeStatisticService;
import com.opc.service.DeviceRecordService;
import com.opc.service.DeviceService;
import com.opc.utils.RedisUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author SEG
 */
@Slf4j
@Component
public class ConsumeStatisticAnalysisSchedule {
    @Resource
    private ConsumeStatisticService statisticService;

    @Resource
    RedisUtils redisUtils;

    @Resource
    private DeviceService deviceService;

    @Resource
    private DeviceRecordService recordService;

    /**
     * 指定每天7点执行
     */
    @Scheduled(cron = "0 0 7 * * ?")
    public void consumeReading() {
        Date currentDate = new Date();
        Date startTime = DateUtil.offsetDay(currentDate, -1);
        List<DeviceRecordTimesDto> list = recordService.selectConsumeByProLineNew(startTime, currentDate, 1);
        if (list == null || list.size() == 1) {
            log.error("没有可统计的设备");
            return;
        }
        List<ConsumeStatistic> statistics = new ArrayList<>();
        for (DeviceRecordTimesDto d : list) {
            ConsumeStatistic consumeStatistic = new ConsumeStatistic();
            consumeStatistic.setStatisticTime(d.getResetTime());
            consumeStatistic.setCreateTime(currentDate);
            consumeStatistic.setProductionLine(d.getProductionLine());
            consumeStatistic.setType(0);
            consumeStatistic.setConsumeVal(d.getResetTimes());
            consumeStatistic.setStationNum(d.getStationNum());
            consumeStatistic.setDeviceCode(d.getDeviceCode());
            statistics.add(consumeStatistic);
        }
        boolean flag = statisticService.saveBatch(statistics);
        if (!flag) {
            log.error("save statistic data occur error datetime: {}", DateUtil.format(new Date(), "yyyy-MM-dd HH:mm:ss"));
        }
    }

    public void consumeReadingTest() {
        Date currentDate = new Date();
        Date endTime = DateUtil.parse("2023-04-27 07:00:00", "yyyy-MM-dd HH:mm:ss");
        for (int i = 0; i <= 14; i++) {
            endTime = DateUtil.offsetDay(endTime, -i);
            Date startTime = DateUtil.offsetDay(endTime, -i - 1);
            List<DeviceRecordTimesDto> list = recordService.selectConsumeByProLineNew(startTime, endTime, i + 1);
            if (list == null || list.size() == 1) {
                log.error("没有可统计的设备");
                return;
            }
            List<ConsumeStatistic> statistics = new ArrayList<>();
            for (DeviceRecordTimesDto d : list) {
                ConsumeStatistic consumeStatistic = new ConsumeStatistic();
                consumeStatistic.setStatisticTime(d.getResetTime());
                consumeStatistic.setCreateTime(currentDate);
                consumeStatistic.setProductionLine(d.getProductionLine());
                consumeStatistic.setType(0);
                consumeStatistic.setConsumeVal(d.getResetTimes());
                consumeStatistic.setStationNum(d.getStationNum());
                consumeStatistic.setDeviceCode(d.getDeviceCode());
                statistics.add(consumeStatistic);
            }
            boolean flag = statisticService.saveBatch(statistics);
            if (!flag) {
                log.error("save statistic data occur error datetime: {}", DateUtil.format(new Date(), "yyyy-MM-dd HH:mm:ss"));
            }
        }

    }
}



